package com.waypolice.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.waypolice.manager.service.ArchiveDetailStatisticsService;
import com.waypolice.mapper.handwriting.ArchiveDetailStatisticsMapperSon;
import com.waypolice.pojo.handwriting.WriteSql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 312小时 on 2019/4/1.
 */
@Service
public class ArchiveDetailStatisticsServiceImpl implements ArchiveDetailStatisticsService {


    @Autowired
    private ArchiveDetailStatisticsMapperSon archivedetailstatisticsmapperson;

    @Override
    public  Object getArchiveDetailStatistics(int offset,int limit) {
        Map<String, Object> result = new HashMap<>();
        String str=" SELECT count(1) AS VOLNUM,v.VOL_NO,v.VOL_TITLE,v.VOL_STORAGE_TIME,v.VOL_ITEM_NUMBER,to_number(v.VOL_TOTAL_PAGE) AS PAGENUM,\n" +
                " v.VOL_AUDIT_LEADER,v.VOL_AUDIT_DATE,v.VOL_INSIDE_DATE,v.VOL_ARCHIVE_YEAR,v.VOL_SECRET,v.VOL_ARCHIVE_UNIT\n" +
                " FROM Cms_Jud_Item c,CMS_JUD_VOLUME v\n" +
                " WHERE c.itme_vol_id=v.vol_id AND v.VOL_ARCHIVE_STATUS=2 " +
                " GROUP BY v.VOL_NO,v.VOL_TITLE,v.VOL_STORAGE_TIME,v.VOL_ITEM_NUMBER,to_number(v.VOL_TOTAL_PAGE),\n" +
                " v.VOL_AUDIT_LEADER,v.VOL_AUDIT_DATE,v.VOL_INSIDE_DATE,v.VOL_ARCHIVE_YEAR,v.VOL_SECRET,v.VOL_ARCHIVE_UNIT\n" +
                " ORDER BY to_number(v.VOL_TOTAL_PAGE)DESC ";
        WriteSql sql=new WriteSql(str);
        List<Object> pageData = archivedetailstatisticsmapperson.getPageData(sql);
        PageHelper.offsetPage(offset, limit, false);
        List<Object> statisticsData = archivedetailstatisticsmapperson.getStatisticsData(sql);
        result.put("rows", statisticsData);
        result.put("total", pageData.size());
        return result;
    }

    @Override
    public Object getPostArchiveDetailStatistics(int offset, int limit, String vol_archive_unit, String vol_archive_year, String vol_storage_time, String vol_secret, String starttime,String endtime) {
        String ext=" AND 1=1 ";
        if(vol_archive_unit!=null&&vol_archive_unit!=""){
            ext+=" AND v.VOL_ARCHIVE_UNIT = '"+vol_archive_unit+"' ";
        }
        if(vol_archive_year!=null&&vol_archive_year!=""){
            ext+=" AND v.VOL_ARCHIVE_YEAR LIKE '%"+vol_archive_year+"%' ";
        }
        if(vol_storage_time!=null&&vol_storage_time!=""){
            ext+=" AND v.VOL_STORAGE_TIME = '"+vol_storage_time+"' ";
        }
        if(vol_secret!=null&&vol_secret!=""){
            ext+=" AND v.VOL_SECRET = '"+vol_secret+"' ";
        }
        if(starttime==null||starttime==""){
            if(endtime!=null&&endtime!=""){
                ext+=" AND v.VOL_ARCHIVE_DATE LIKE '%"+endtime+"%' ";
            }

        }else{
            if(endtime!=null&&endtime!=""){
                ext+=" AND v.VOL_ARCHIVE_DATE >=  to_date('"+starttime+"','yyyy-MM-dd') AND v.VOL_ARCHIVE_DATE <=to_date('"+endtime+"','yyyy-MM-dd') ";
            }else{
                ext+=" AND v.VOL_ARCHIVE_DATE LIKE '%"+starttime+"%' ";
            }
        }
        Map<String, Object> result = new HashMap<>();
        String str=" SELECT count(1) AS VOLNUM,v.VOL_NO,v.VOL_TITLE,v.VOL_STORAGE_TIME,v.VOL_ITEM_NUMBER,to_number(v.VOL_TOTAL_PAGE) AS PAGENUM,\n" +
                " v.VOL_AUDIT_LEADER,v.VOL_AUDIT_DATE,v.VOL_INSIDE_DATE,v.VOL_ARCHIVE_YEAR,v.VOL_SECRET,v.VOL_ARCHIVE_UNIT\n" +
                " FROM Cms_Jud_Item c,CMS_JUD_VOLUME v\n" +
                " WHERE c.itme_vol_id=v.vol_id  AND v.VOL_ARCHIVE_STATUS=2 " +ext+
                " GROUP BY v.VOL_NO,v.VOL_TITLE,v.VOL_STORAGE_TIME,v.VOL_ITEM_NUMBER,to_number(v.VOL_TOTAL_PAGE),\n" +
                " v.VOL_AUDIT_LEADER,v.VOL_AUDIT_DATE,v.VOL_INSIDE_DATE,v.VOL_ARCHIVE_YEAR,v.VOL_SECRET,v.VOL_ARCHIVE_UNIT\n" +
                " ORDER BY to_number(v.VOL_TOTAL_PAGE)DESC ";
        WriteSql sql=new WriteSql(str);
        List<Object> pageData = archivedetailstatisticsmapperson.getPageData(sql);
        PageHelper.offsetPage(offset, limit, false);
        List<Object> statisticsData = archivedetailstatisticsmapperson.getStatisticsData(sql);
        result.put("rows", statisticsData);
        result.put("total", pageData.size());
        return result;
    }
}
